﻿// Author:					HungNM
// Created:					27-07-2012
// Last Modified:			27-07-2012
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI;

namespace eProject.Business
{
    public class ScriptHelper
    {
        public ScriptHelper()
        {

        }

        private bool includeJquery;

        public bool IncludeJquery
        {
            get { return includeJquery; }
            set
            {
                includeJquery = value;
                if (includeJquery) { SetupIncludeJquery(); }
            }
        }

        private bool includeJqueryUICore;

        public bool IncludeJqueryUICore
        {
            get { return includeJqueryUICore; }
            set
            {
                includeJqueryUICore = value;
                if (includeJqueryUICore) { SetupIncludeJqueryUICore(); }
            }
        }

        private bool includeSmoothMenu;

        public bool IncludeSmoothMenu
        {
            get { return includeSmoothMenu; }
            set
            {
                includeSmoothMenu = value;
                if (includeSmoothMenu) { SetupIncludeSmoothMenu(); }
            }
        }

        private bool includeJqueryMaskedInput;

        public bool IncludeJqueryMaskedInput
        {
            get { return includeJqueryMaskedInput; }
            set
            {
                includeJqueryMaskedInput = value;
                if (includeJqueryMaskedInput) { SetupIncludeJqueryMasedInput(); }
            }
        }

        private void SetupIncludeSmoothMenu()
        {
            if (includeSmoothMenu)
            {
                AddScriptFile("ddsmoothmenu.js", "/js/");
            }
        }

        private void SetupIncludeJqueryUICore()
        {
            if (includeJqueryUICore)
            {
                AddScriptFile("jquery-ui-1.8.23.custom.min.js", "/js/");
            }
        }


        private void SetupIncludeJquery()
        {
            if (includeJquery)
            {
                AddScriptFile("jquery-1.8.0.min.js", "/js/");
            }
        }

        private void SetupIncludeJqueryMasedInput()
        {
            if (includeJqueryMaskedInput)
            {
                AddScriptFile("jquery.maskedinput-1.3.min.js", "/js/");
            }
        }

        private static void AddScriptFile(string File, string Directory)
        {
            System.Web.UI.Page CurrentPage = (System.Web.UI.Page)HttpContext.Current.CurrentHandler;
            bool Added = false;

            foreach (Control CurrentControl in CurrentPage.Header.Controls)
            {
                if (CurrentControl.GetType() == typeof(HtmlGenericControl))
                {
                    HtmlGenericControl HTMLGenericControl = (HtmlGenericControl)CurrentControl;
                    if (HTMLGenericControl.Attributes["src"] != null)
                    {
                        if (HTMLGenericControl.Attributes["src"].Contains(File))
                        {
                            Added = true;
                        }
                    }
                }
            }

            if (!Added)
            {
                HtmlGenericControl ScriptControl = new HtmlGenericControl("script");
                ScriptControl.Attributes.Add("type", "text/javascript");
                ScriptControl.Attributes.Add("src", Directory + File);
                CurrentPage.Header.Controls.Add(ScriptControl);
            }
        }
    }
}
